//
//  ImageNewsViewController.m
//  Mtime
//
//  Created by Mac on 15/8/10.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "ImageNewsViewController.h"
#import "ImageNewsHeader.h"

#import "ImageScanViewController.h"//大图浏览

@interface ImageNewsViewController ()
{

    NSMutableArray *_imageDataList;
    NSMutableArray *_imageDataList2;
    
    NSDictionary *_headerDic;//头部视图的数据
}
@end

@implementation ImageNewsViewController

- (void)reloadView {
 
    _imageV1.image = [[UIImage imageNamed:@"pic_movielist_bg.9"] stretchableImageWithLeftCapWidth:20 topCapHeight:50];
    _imageV2.image = [[UIImage imageNamed:@"pic_movielist_bg.9"] stretchableImageWithLeftCapWidth:20 topCapHeight:50];
    _imageV3.image = [[UIImage imageNamed:@"pic_movielist_bg.9"] stretchableImageWithLeftCapWidth:20 topCapHeight:50];
    
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"imageCell"];
    
    
    ImageNewsHeader *headeView = [[[NSBundle mainBundle] loadNibNamed:@"ImageNewsHeader" owner:nil options:nil] lastObject];
    headeView.dataDic = _headerDic;
    
    self.tableView.tableHeaderView = headeView;
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_top_movie_background_cover"]];
    
    [self loadData];
    
    [self reloadView];
}


- (void)loadData {
 
    /*
     "type": 1,
     "id": 1544622,
     "title": "《桂宝》动画大电影曝新海报剧照",
     "title2": "中国好桂宝癫疯对决拖拉基",
     "content": "随着8月6日上映日期的临近，动画电影《桂宝之爆笑闯宇宙》继“三胖接力”海报后，首次曝光一张“中国好《桂宝》”战队“癫疯”对決“宇宙拖拉基”战队之两派对立海报。同时，还曝光了一系列“机器人”剧照档案，泄露了机器人的独特造型，他们都各怀技能。备战状态下，斗场上充满了火药味，疯狂宇宙发明大赛即将打响。",
     "time": "2015-7-14 11:53:54"
     "source": "时光网通稿",
     "author": "JJ 艾布拉迪奥",
     "editor": "Aska，克里斯 派恩，扎克瑞 昆图",
     
     "relations": [
     {
     "type": 1,
     "id": 220081,
     "name": "桂宝之爆笑闯宇宙",
     "image": "http://img31.mtime.cn/mt/2015/07/02/111644.56181130_1280X720X2.jpg",
     "year": "2015",
     "rating": -1,
     "scoreCount": 13,
     "releaseDate": "2015年8月6日",
     "relaseLocation": "中国"
     }

     
     
     "images": [
     {
     "gId": 476340,
     "title": "",
     "desc": "宝·基“癫疯”对决海报",
     "url1": "http://img31.mtime.cn/CMS/Gallery/2015/07/14/115420.19814860.jpg",
     "url2": "http://img31.mtime.cn/CMS/Gallery/2015/07/14/115420.19814860_900.jpg"
     },

     */
    
    NSDictionary *imageNewsDic = [CoreDataSource initWithDataSource:@"image_news"];
    

    /*----------------头部视图数据----------------*/
    NSArray *relations = [imageNewsDic objectForKey:@"relations"];
    
    _headerDic = [relations objectAtIndex:0];
    
    
    
    
    /*----------------分组1:电影剧情----------------*/
    _label1.text = [imageNewsDic objectForKey:@"content"];
    
    
    
    /*----------------分组2:演职人员----------------*/
    _label2.text = [NSString stringWithFormat:@"导演：%@",[imageNewsDic objectForKey:@"author"]];
    
    _label3.text = [NSString stringWithFormat:@"演员：%@",[imageNewsDic objectForKey:@"author"]];
    
    
    
    
    
    /*----------------分组3:电影剧照----------------*/
    NSArray *images = [imageNewsDic objectForKey:@"images"];
    
    _imageDataList = [[NSMutableArray alloc] init];
    
    _imageDataList2 = [[NSMutableArray alloc] init];
    
    for (NSDictionary *dic in images) {
        
        NSString *imageURL = [dic objectForKey:@"url1"];
        
        NSString *imageURL2 = [dic objectForKey:@"url2"];
        
        [_imageDataList addObject:imageURL];
        
        [_imageDataList2 addObject:imageURL2];
        
    }
}

#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _imageDataList.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
    
    [imageV setImageWithURL:[NSURL URLWithString:_imageDataList[indexPath.item]]];
    
    [cell.contentView addSubview:imageV];
    
    return cell;

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    
    ImageScanViewController *imageScanVC = [[ImageScanViewController alloc] init];
    
    imageScanVC.imageDataList = _imageDataList2;
    
    //将选中图片在collectionview中所处的位置传入ImageScanViewController
    imageScanVC.currentIndexPath = indexPath;
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:imageScanVC];
    
    [self presentViewController:navi animated:YES completion:nil];
    
}

@end
